﻿@{
    Layout = "~/Views/Shared/_LayoutPageEmpty.cshtml";
}
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=mTePrDFVkrXBzMWmNCdemNTp"></script>
<div id="allmap" style="height:500px;"></div>
<div style="position: absolute;top:20px;left:200px;">
    <div id="r-result">
        <input type="text" id="suggestId" size="20" class="inp-text bc1 bgc0 inp-lg10 inp-hg6" />
        <a href="javascript:void(0)" class="l-btn submit btn-lg5 btn-hg6" id="BtnUserAddress">使用该地址</a>
    </div>
    <div id="searchResultPanel" style="border:1px solid #C0C0C0;width:150px;height:auto; display:none;"></div>
</div>
<script type="text/javascript">
    $(function () {
        //百度地图API功能
        var map = new BMap.Map("allmap");    // 创建Map实例
        map.centerAndZoom(new BMap.Point(116.404, 39.915), 11);  // 初始化地图,设置中心点坐标和地图级别

        map.addControl(new BMap.MapTypeControl());//添加地图类型控件
        map.addControl(new BMap.NavigationControl());//添加地图缩放控件
        //map.addControl(new BMap.ScaleControl());
        map.addControl(new BMap.OverviewMapControl());
        map.setDefaultCursor("url('bird.cur')");   //设置地图默认的鼠标指针样式
        map.enableScrollWheelZoom(true);//开启鼠标滚轮缩放


        var w = $("body").width();
        var h = $("body").height();
        // 定义一个控件类,即function
        function ZoomControl() {
            // 默认停靠位置和偏移量
            this.defaultAnchor = BMAP_ANCHOR_TOP_LEFT;
            this.defaultOffset = new BMap.Size(w / 2 - 15, h / 2 - 44);
        }
        // 通过JavaScript的prototype属性继承于BMap.Control
        ZoomControl.prototype = new BMap.Control();
        // 自定义控件必须实现自己的initialize方法,并且将控件的DOM元素返回
        // 在本方法中创建个div元素作为控件的容器,并将其添加到地图容器中
        ZoomControl.prototype.initialize = function (map) {
            // 创建一个DOM元素
            var div = document.createElement("div");
            // 添加文字说明
            //div.appendChild(document.createTextNode("放大2级"));
            // 设置样式
            div.style.cursor = "pointer";
            //div.style.border = "1px solid gray";
            //div.style.backgroundColor = "red";
            div.style.backgroundImage = "url(/images/32.png)";
            div.style.backgroundPosition = "0 0";
            div.style.backgroundRepeat = "no-repeat";
            div.style.width = "30px";
            div.style.height = "44px";

            // 绑定事件,点击一次放大两级
            div.onclick = function (e) {
                //map.setZoom(map.getZoom() + 2);
            }
            // 添加DOM元素到地图中
            map.getContainer().appendChild(div);
            // 将DOM元素返回
            return div;
        }
        // 创建控件
        var myZoomCtrl = new ZoomControl();
        // 添加到地图当中
        map.addControl(myZoomCtrl);



        

        //map.addEventListener("click", function (e) {
        //    alert(e.point.lng + "," + e.point.lat);
        //});
        var $address = $("#Address", window.parent.document);
        var $X = $("#X", window.parent.document);
        var $Y = $("#Y", window.parent.document);
        var $IsMapped = $("#IsMapped", window.parent.document);
        var $WindowMap = $("#Window-Map", window.parent.document);
        
        //#region 根据ip定位当前城市
        function myFun(result) {
            //alert(result.center.lng + "---" + result.center.lat);
            //alert("当前地图中心点：" + map.getCenter().lng + "," + map.getCenter().lat);
            //var marker = new BMap.Marker(new BMap.Point(result.center.lng, result.center.lat));  // 创建标注
            //map.addOverlay(marker);
            //根据用用户输入地址
            var address = $address.val();
            var x = $X.val();
            var y = $Y.val();
            
            if ($IsMapped.val() && $IsMapped.val()=="True") {
                map.centerAndZoom(new BMap.Point(x, y), 20);
                return;
            } 
            if ($.trim(address)) {
                //map.centerAndZoom(address, 11);
                $("#suggestId").val(address);
                //创建地址解析器实例
                var myGeo = new BMap.Geocoder();
                // 将地址解析结果显示在地图上,并调整地图视野
                myGeo.getPoint(address, function (point) {
                    if (point) {
                        map.centerAndZoom(point, 20);
                    } else {
                        var cityName = result.name;
                        map.setCenter(cityName);
                    }
                });
            } else {
                var cityName = result.name;
                map.setCenter(cityName);
            }
            
        }
        var myCity = new BMap.LocalCity();
        myCity.get(myFun);
        //#endregion

        var geoc = new BMap.Geocoder();
        map.addEventListener("click", function (e) {
            var pt = e.point;
            geoc.getLocation(pt, function (rs) {
                var addComp = rs.addressComponents;
                var address = addComp.province + addComp.city + addComp.district + addComp.street + addComp.streetNumber;
            });
        });
        map.addEventListener("dragend", function () {
            var center = map.getCenter();
            //alert("地图中心点变更为：" + center.lng + ", " + center.lat);
            geoc.getLocation(center, function (rs) {
                var addComp = rs.addressComponents;
                var address = addComp.province + addComp.city + addComp.district + addComp.street + addComp.streetNumber;
                $("#suggestId").val(address);
            });
        });

        //建立一个自动完成的对象
        var ac = new BMap.Autocomplete({ "input": "suggestId", "location": map });
        //鼠标放在下拉列表上的事件
        ac.addEventListener("onhighlight", function (e) {
            var str = "";
            var _value = e.fromitem.value;
            var value = "";
            if (e.fromitem.index > -1) {
                value = _value.province + _value.city + _value.district + _value.street + _value.business;
            }
            str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;

            value = "";
            if (e.toitem.index > -1) {
                _value = e.toitem.value;
                value = _value.province + _value.city + _value.district + _value.street + _value.business;
            }
            str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
            $("#searchResultPanel").html(str);
        });
        var myValue;
        ac.addEventListener("onconfirm", function (e) {    //鼠标点击下拉列表后的事件
            var _value = e.item.value;
            myValue = _value.province + _value.city + _value.district + _value.street + _value.business;
            $("#searchResultPanel").html("onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue);
            setPlace();
        });
        function setPlace() {
            map.clearOverlays();    //清除地图上所有覆盖物
            function myFun() {
                var pp = local.getResults().getPoi(0).point;    //获取第一个智能搜索的结果
                map.centerAndZoom(pp, 20);
                //map.addOverlay(new BMap.Marker(pp));    //添加标注
            }
            var local = new BMap.LocalSearch(map, { //智能搜索
                onSearchComplete: myFun
            });
            local.search(myValue);
        }

        $("#BtnUserAddress").bind("click", function () {
            $address.val($("#suggestId").val());
            var lng = map.getCenter().lng;
            var lat = map.getCenter().lat;
            $X.val(lng);
            $Y.val(lat);
            $IsMapped.val(true);
            $WindowMap.CloseWindow();
        });
    });
</script>